package com.xhh.nphm.common.utils.sms;

import com.xhh.nphm.common.utils.StringUtils;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.net.URL;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class SmsUtils {
    @Value("${sms.smsUrl}")
    public String smsUrl;

    public static String reqUrl;

    @PostConstruct
    public void init() {
        reqUrl = smsUrl;
    }

    public static String sendStatusOK = "0";

    public static String sendStatusFail = "验证码获取失败";

    public static String verifyStatusFail = "手机验证码错误";

    public static String smsCodeTimeOut = "验证码获取超时";

    public static String sendStatusError = "ERROR";

    public static String sendStatusForbidden = "获取频繁";

    public static int intervalTime = 1;

    public static int expireTime = 300;

    private static final Pattern pattern = Pattern.compile("^1[3-9]\\d{9}$");

    public static boolean sendSmsCode(String mobile, String checkCode) {
        boolean flag = false;
        try {
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new URL(reqUrl));
            call.setOperation("InterRequestH");
            call.setMaintainSession(true);
            String ret = (String) call.invoke(new Object[]{mobile, checkCode});
            if (StringUtils.isNotBlank(ret)) {
                String md5en = ret.substring(ret.indexOf(":") + 1, ret.length());
                System.out.println(md5en);
                if (StringUtils.isNotEmpty(md5en))
                    flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    //短信接口
    public static boolean workFormNote(String mobile, String content) {
//        System.err.println(content);
        boolean flag = false;
        try {
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new URL(reqUrl));
            call.setOperation("InterRequestH");
            call.setMaintainSession(true);
            String ret = (String) call.invoke(new Object[]{mobile, content});
            if (StringUtils.isNotBlank(ret)) {
                String md5en = ret.substring(ret.indexOf(":") + 1, ret.length());
                System.out.println(md5en);
                if (StringUtils.isNotEmpty(md5en))
                    flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public static String generateCode() {
        Random random = new Random();
        char[] ch = "0123456789".toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            int index = random.nextInt(ch.length);
            sb.append(ch[index]);
        }
        return sb.toString();
    }

    public static boolean isMobile(String mobile) {
        boolean b = false;
        if (StringUtils.isNotBlank(mobile)) {
            Matcher matcher = pattern.matcher(mobile);
            b = matcher.matches();
        }
        return b;
    }
}
